package org.example;

import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.resps.Tuple;

import java.util.List;

public class ZsetDemo {
    public static void main(String[] args) {
        JedisPool jedisPool = new JedisPool("tcp://127.0.0.1:8888");
        try (Jedis jedis = jedisPool.getResource()) {
            //删除数据库
            jedis.flushAll();
            //测试代码如下
            //testZaddAndZrange(jedis);
            //testZremAndZcard(jedis);
            //testZcount(jedis);
            //testZpopmaxAndZpopmin(jedis);
            //testZscore(jedis);
            //testZincrby(jedis);
            //testZinterstore(jedis);
            //testZunionstore(jedis);
        }
    }
    private static void testZaddAndZrange(Jedis jedis) {
        System.out.println("测试 zadd 和 zrange");
        jedis.zadd("key", 100, "吕布");
        jedis.zadd("key", 98, "赵云");
        jedis.zadd("key", 95, "典⻙");
        jedis.zadd("key", 92, "关⽻");
        jedis.zadd("key", 70, "刘备");
        List<String> members = jedis.zrange("key", 0, 4);
        System.out.println(members);
        List<Tuple> membersWithScore = jedis.zrangeWithScores("key", 0, 4);
        System.out.println(membersWithScore);
    }
    private static void testZremAndZcard(Jedis jedis) {
        System.out.println("测试 zrem 和 zcard");
        jedis.zadd("key", 100, "吕布");
        jedis.zadd("key", 98, "赵云");
        jedis.zadd("key", 95, "典⻙");
        jedis.zadd("key", 92, "关⽻");
        jedis.zadd("key", 70, "刘备");
        long n = jedis.zcard("key");
        System.out.println(n);
        n = jedis.zrem("key", "吕布", "赵云");
        System.out.println(n);
        n = jedis.zcard("key");
        System.out.println(n);
    }
    private static void testZcount(Jedis jedis) {
        System.out.println("测试 zount ");
        jedis.zadd("key", 100, "吕布");
        jedis.zadd("key", 98, "赵云");
        jedis.zadd("key", 95, "典⻙");
        jedis.zadd("key", 92, "关⽻");
        jedis.zadd("key", 70, "刘备");
        long n = jedis.zcount("key", 92, 98);
        System.out.println(n);
    }
    private static void testZpopmaxAndZpopmin(Jedis jedis) {
        System.out.println("测试 zpopmax 和 zpopmin");
        jedis.zadd("key", 100, "吕布");
        jedis.zadd("key", 98, "赵云");
        jedis.zadd("key", 95, "典⻙");
        jedis.zadd("key", 92, "关⽻");
        jedis.zadd("key", 70, "刘备");
        Tuple tuple = jedis.zpopmax("key");
        System.out.println("最大" + tuple);
        tuple = jedis.zpopmin("key");
        System.out.println("最小" + tuple);
    }
    private static void testZrank(Jedis jedis) {
        System.out.println("测试 zrank");
        jedis.zadd("key", 100, "吕布");
        jedis.zadd("key", 98, "赵云");
        jedis.zadd("key", 95, "典⻙");
        jedis.zadd("key", 92, "关⽻");
        jedis.zadd("key", 70, "刘备");
        long n = jedis.zrank("key", "赵云");
        System.out.println(n);
        n = jedis.zrevrank("key", "赵云");
        System.out.println(n);
    }
    private static void testZscore(Jedis jedis) {
        System.out.println("测试 zscore");
        jedis.zadd("key", 100, "吕布");
        jedis.zadd("key", 98, "赵云");
        jedis.zadd("key", 95, "典⻙");
        jedis.zadd("key", 92, "关⽻");
        jedis.zadd("key", 70, "刘备");
        double score = jedis.zscore("key", "赵云");
        System.out.println(score);
    }
    private static void testZincrby(Jedis jedis) {
        System.out.println("测试 zincrby");
        jedis.zadd("key", 100, "吕布");
        double n = jedis.zincrby("key", 10, "吕布");
        System.out.println(n);
        n = jedis.zincrby("key", -20, "吕布");
        System.out.println(n);
    }
    private static void testZinterstore(Jedis jedis) {
        System.out.println("测试 zinterstore");
        jedis.zadd("key1", 100, "吕布");
        jedis.zadd("key1", 98, "赵云");
        jedis.zadd("key1", 95, "典⻙");
        jedis.zadd("key2", 100, "吕布");
        jedis.zadd("key2", 98, "赵云");
        jedis.zadd("key2", 92, "关⽻");
        long n = jedis.zinterstore("key3", "key1", "key2");
        System.out.println(n);
        List<Tuple> tuples = jedis.zrangeWithScores("key3", 0, -1);
        System.out.println(tuples);
    }
    private static void testZunionstore(Jedis jedis) {
        System.out.println("测试 zunionstore ");
        jedis.zadd("key1", 100, "吕布");
        jedis.zadd("key1", 98, "赵云");
        jedis.zadd("key1", 95, "典⻙");
        jedis.zadd("key2", 100, "吕布");
        jedis.zadd("key2", 98, "赵云");
        jedis.zadd("key2", 92, "关⽻");
        long n = jedis.zunionstore("key3", "key1", "key2");
        System.out.println(n);
        List<Tuple> tuples = jedis.zrangeWithScores("key3", 0, -1);
        System.out.println(tuples);
    }
}
